package com.hsq.spzx.manager.service.impl;

import com.hsq.spzx.common.exception.HsqException;
import com.hsq.spzx.manager.mapper.user.SysMenuMapper;
import com.hsq.spzx.manager.mapper.user.SysRoleMenuMapper;
import com.hsq.spzx.manager.service.SysMenuService;
import com.hsq.spzx.manager.utils.MenuHelper;
import com.hsq.spzx.model.entity.system.SysMenu;
import com.hsq.spzx.model.entity.system.SysUser;
import com.hsq.spzx.model.vo.common.ResultCodeEnum;
import com.hsq.spzx.model.vo.system.SysMenuVo;
import com.hsq.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hsq
 * @date 2025-06-04 下午6:04
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenuList = sysMenuMapper.selectAll();
        if (CollectionUtils.isEmpty(sysMenuList))
            return null;
        return MenuHelper.buildTree(sysMenuList); //构建树形数据
    }

    @Transactional
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.insert(sysMenu);

        // 新添加了一子个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenu(sysMenu);
    }

    private void updateSysRoleMenu(SysMenu sysMenu) {
        //得到当前的父菜单
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if (parentMenu != null) {
            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
            //递归调用
            updateSysRoleMenu(parentMenu);
        }
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu) ;
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.countByParentId(id);  // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0) {
            throw new HsqException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.deleteById(id);		// 不存在子菜单直接删除
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId);
        //构建树形数据
        List<SysMenu> sysMenus = MenuHelper.buildTree(sysMenuList);
        return this.buildMenus(sysMenus);
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {
        List<SysMenuVo> sysMenuVos = new ArrayList<>();
        for (SysMenu menu: menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(menu.getTitle());
            sysMenuVo.setName(menu.getComponent());
            List<SysMenu> children = menu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVos.add(sysMenuVo);
        }
        return sysMenuVos;
    }

}
